
mod teapot;

use std::time::{Instant, Duration};
use cgmath::{Vector3, Matrix4, Rad};

use self::teapot::{TEAPOT, INDICES, NORMALS};

#[derive(Copy, Clone)]
pub struct Vertex {
    position: (f32, f32, f32)
}

impl_vertex!(Vertex, position);

#[derive(Copy, Clone)]
pub struct Normal {
    normal: (f32, f32, f32)
}

impl_vertex!(Normal, normal);

pub struct Teapot{
    pub indices: [u16; 3072],
    pub normals: [Normal; 531],
    pub vertexes: [Vertex; 531],
    scale: Matrix4<f32>,
    rotate: Matrix4<f32>,
    transl: Matrix4<f32>,
    create: Instant
}

impl Teapot{
    pub fn new() -> Teapot{
        Teapot{
            indices: INDICES,
            normals: NORMALS,
            vertexes: TEAPOT,
            scale: Matrix4::from_scale(0.01f32),
            rotate: Matrix4::from_angle_y(Rad(0.0f32)),
            transl: Matrix4::from_translation(Vector3::new(0f32, 0f32, 0f32)),
            create: Instant::now()
        }
    }

    pub fn scale(&self) -> Matrix4<f32>{
        self.scale
    }

    pub fn rotate(&self) -> Matrix4<f32>{
        //绕y轴旋转
        let elapsed = self.create.elapsed();
        let rotation = elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1_000_000_000.0;
        Matrix4::from_angle_y(Rad(rotation as f32))
    }

    pub fn translate(&self) -> Matrix4<f32>{
        self.transl
    }
}

